package com.across.connect.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.IllegalAddException;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.across.connect.model.Plan;
import com.across.connect.util.CommonConst;

public class PlanXmlParser {
 
	private static String xmlfile = CommonConst.DB_PLAN_XML;

	public static Document getRoot() throws JDOMException, IOException {
		SAXBuilder builder = new SAXBuilder();
		InputStream file = new FileInputStream(xmlfile);
		Document document = builder.build(file);
		return document;
	}

	public static boolean addPlanXml(String personid,
			String content, String date) throws JDOMException,
			FileNotFoundException, IOException, IllegalAddException {

		Document document;
		Element root;

		File myFilePath = new File(xmlfile);
		if (!myFilePath.exists()) {
			myFilePath.createNewFile();
			root = new Element("user_plan");
			document = new Document(root);
			XMLOutputter XMLOut = new XMLOutputter();
			XMLOut.output(document, new FileOutputStream(xmlfile));
		} else {
			SAXBuilder builder = new SAXBuilder();
			InputStream file = new FileInputStream(xmlfile);
			document = builder.build(file);
			root = document.getRootElement();
		}

		Element userelement = getPersonByUserid(Integer.valueOf(personid));

		userelement = new Element("user");
		userelement.setAttribute("userid", personid);

		int maxid = getPlanMaxIdByUser(Integer.valueOf(personid));
		userelement.setAttribute("planid", String.valueOf(++maxid));

		Element element1 = new Element("content");
		element1.setText(content);
		Element element2 = new Element("date");
		element2.setText(date);

		userelement.addContent(element1);
		userelement.addContent(element2);
		userelement.detach();
		root.addContent(userelement);

		Format format = Format.getCompactFormat();
		format.setIndent("    ");
		XMLOutputter XMLOut = new XMLOutputter(format);

		XMLOut.output(document, new FileOutputStream(xmlfile));
		return true;

	}

	public static List<Plan> getPlanUserByUserid(int userid)
			throws JDOMException, IOException {
		List<Element> list = getRoot().getRootElement().getChildren();
		List<Plan> one = new ArrayList<Plan>();
		for (Element e : list) {
			if (Integer.parseInt(e.getAttributeValue("userid")) == userid) {
				Plan two = new Plan();
				two.setPersonid(Integer.valueOf(e.getAttributeValue("userid")));
				two.setId(Integer.valueOf(e.getAttributeValue("planid")));
				two.setContent(e.getChildText("content"));
				two.setDate(e.getChildText("date"));
				one.add(two);
			}
		}
		return one;
	}
	@SuppressWarnings("unchecked")
	public static Plan getLatestPlanByUserid(int userid)
			throws JDOMException, IOException {
		List<Element> list = getRoot().getRootElement().getChildren();
		Plan plan = null;
		int maxid = getPlanMaxIdByUser(userid);
		if (maxid < 1) {
			return null;
		}
		for (Element e : list) {
			if (Integer.parseInt(e.getAttributeValue("planid")) == maxid) {
				plan = new Plan();
				plan.setPersonid(Integer.valueOf(e.getAttributeValue("userid")));
				plan.setId(Integer.valueOf(e.getAttributeValue("planid")));
				plan.setContent(e.getChildText("content"));
				plan.setDate(e.getChildText("date"));
				break;
			}
		}
		return plan;
	}
	@SuppressWarnings("unchecked")
	public static boolean getPlanUser(int userid) throws JDOMException,
			IOException {
		List<Element> list = getRoot().getRootElement().getChildren();
		for (Element e : list) {
			if (Integer.parseInt(e.getAttributeValue("userid")) == userid) {
				return true;
			}
		}
		return false;
	}

	// 获取一个用户计划的 最大planid号
	@SuppressWarnings("unchecked")
	public static int getPlanMaxIdByUser(int userid)
			throws JDOMException, IOException {
		List<Element> list = getRoot().getRootElement().getChildren();
		List<Integer> idlist = new ArrayList<Integer>();
		List<Element> planlist = new ArrayList<Element>();
		for (Element e : list) {
			if (Integer.parseInt(e.getAttributeValue("userid")) == userid) {
				planlist.add(e);
			}
		}
		if (planlist.size() < 0)
			return 0;
		for (Element e : planlist) {
			idlist.add(Integer.parseInt(e.getAttributeValue("planid")));
		}
		Comparator comp = new Comparator() {
			public int compare(Object o1, Object o2) {
				int p1 = (Integer) o1;
				int p2 = (Integer) o2;
				if (p1 < p2)
					return 1;
				else
					return 0;
			}
		};
		Collections.sort(idlist, comp);
		if (idlist.size() > 0) {
			return idlist.get(0);
		} else
			return 0;
	}

	public static Element getPersonByUserid(int userid)
			throws JDOMException, IOException {
		List<Element> list = getRoot().getRootElement().getChildren();
		for (Element e : list) {
			if (Integer.parseInt(e.getAttributeValue("userid")) == userid) {
				return e;
			}
		}
		return null;
	}

 

	public static void main(String args[]) {
		try {

			// System.out.println(PlanXmlParser.getPersonByUserid(1));

			// System.out.println(PlanXmlParser.getPlanMaxIdByUser(1));

			for (Plan e : PlanXmlParser.getPlanUserByUserid(3)) {
				System.out.println("===========" + e.getContent());
			}
 

			/*
			 * System.out.println(PlanXmlParser.getPlanUser(3)); Date date = new
			 * Date();
			 */
			/*
			 * if (PlanXmlParser.addPlanXml("2", "我们头一次来做这件事情，真的好好高兴呀",
			 * DateFormat .getDateInstance().format(date)))
			 * System.out.println("增加成功！！！！！！！！！！！！");
			 */
			// PlanXmlParser.addPlanXml("2", "hello world 66666666",
			// "2009/05/08");
		} catch (IllegalAddException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	


}
